<?php
//导入vendor/autoload.php文件
//require('./vendor/autoload.php');
//引入命名空间
/*use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;*/

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer-6.1.5/src/Exception.php';
require 'PHPMailer-6.1.5/src/PHPMailer.php';
require 'PHPMailer-6.1.5/src/SMTP.php';
require_once('config.inc.php');//$pdo; $uid; ROOT

//测试数据
//$_POST = ['email'=>'huodeming1@163.com','code'=>$_SESSION['verify']];
//非法操作
if(empty($_POST['email']) || empty($_POST['code'])){
	$result = ['success'=>0,'msg'=>'0非法操作!'];
	die(json_encode($result));
}
$email = trim($_POST['email']);

$pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
//验证邮件地址格式
if(!filter_var($email, FILTER_VALIDATE_EMAIL) || !preg_match($pattern, $email)){
    $result = ['success'=>0,'msg'=>'1真不知说什么好,注册时输入的邮箱地址不是有效的邮箱地址!'];
	die(json_encode($result));
}

//验证码是否正确
if(strtolower($_POST['code']) != strtolower($_SESSION['verify'])){
	$result = ['success'=>0,'msg'=>'2验证码不正确!'];
	die(json_encode($result));
}

//查询数据库===生成链接地址==========
$sql = "SELECT uname,email FROM kp_usr WHERE email='{$email}'";
$stmt = $pdo->query($sql);
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
//print_r($result);
if(count($res) != 1){
	$result = ['success'=>0,'msg'=>'3输入的邮箱地址没有注册!!!'];
	die(json_encode($result));
}else{
	$tmpurl = 'http://kpit.vip/password1.php?uname='.$res[0]['uname'].'&toke='.md5($email).'&key='.(time()+60*60*24);
	$url = '请点击该链接进行重置密码<a href="'.$tmpurl.'">'.$tmpurl.'</a>如果不能不能点击跳车,请复制链接地址到浏览器中打开!';
	$url1 = $tmpurl."\r\n".'请复如上制链接地址到浏览器中打开后重设密码!谢谢';
	//echo $url;
}


//die(json_encode(['success'=>1,'msg'=>'000发送成功!000']));
//创建PHPMailer对象.
$mail = new PHPMailer(true);

try {
//关于服务设置的项
    //开启调试模式,项目开发时开启,上线时关闭
    //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    //使用SMTP协议来发送邮件
    $mail->isSMTP();                                            // Send using SMTP
    //设置SMTP的域名(自己的)
    $mail->Host       = 'smtp.exmail.qq.com';                    // Set the SMTP server to send through
    //开启权限认证
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    //用户名
    $mail->Username   = 'admin@kpit.vip';                     // SMTP username
    //密码
    $mail->Password   = 'Hdm790916';                               // SMTP password
    //启用SMTP(TLS)加密
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    //SMTP服务器端口
    $mail->Port       = 465;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

//Recipients
    //发件人邮箱,自定义名称
    $mail->setFrom('admin@kpit.vip', 'admin');
    //收件人邮箱,自定义名称
    //$mail->addAddress('huodeming@163.com', 'hello');     // Add a recipient
    $mail->addAddress($email, 'hello');     // Add a recipient
    
    //$mail->addAddress('56928691@qq.com', 'me');
    //$mail->addAddress('ellen@example.com');               // Name is optional
    //当发送成功,给此人的消息(可能是回执)
    //$mail->addReplyTo('info@example.com', 'Information');
    //抄送给其他人...
    //$mail->addCC('huodeming@163.com');
    //$mail->addBCC('bcc@example.com');

// Attachments
    //给邮件添加附件
    //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

// Content
	//是否开启HTML格式显示,开启后你可以用HTML代码
    $mail->isHTML(true);                                  // Set email format to HTML
    //邮件标题
    $mail->Subject = 'kpit.vip网站找回密码邮件';
    //邮件的内容
    //$mail->Body    = 'This is the HTML message body <b>in bold!</b>'.'测试发送1';
	$mail->Body    = $url;
	//如果不支持HTML显示,则不显示此内容.
    //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
	$mail->AltBody = $url1;
	//发送
    $mail->send();
    //echo '发送成功';
    $result = ['success'=>1,'msg'=>'发送成功!'];
} catch (Exception $e) {
    //echo "发送失败,详情: {$mail->ErrorInfo}";
    $result = ['success'=>0,'msg'=>'发送失败!是发送邮箱验证错误,请联系本站管理员!'];
}
echo json_encode($result);
?>